/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package counter.stosc.com;

import javax.microedition.lcdui.*;

/**
 *
 * @author Stosc
 */
class JobConterUI extends Canvas implements CommandListener {

    private Controller controller;
    private Counter counter;
    private Command complete = new Command("结束", Command.BACK, 0);
    private Command count = new Command("计数", Command.ITEM, 1);
    private Font f;
    private int y = 0;
    private int x = getWidth()/2;
//    private Graphics rg;
    

    public JobConterUI(Controller controller) {
//        super("修学计数器");
         this.controller = controller;
        setSize(Font.SIZE_MEDIUM);
        this.controller = controller;
        this.addCommand(complete);
        this.addCommand(count);
        setCommandListener(this);
    }

    public void load(Counter counter) {
        this.counter = counter;
//        counterName.setText(counter.getCounterName());
//        scheduleCount.setText(String.valueOf(counter.getScheduleCount()));
//        completeConut.setText(String.valueOf(counter.getCompleteConut()));
//        completePercentum.setText(String.valueOf(counter.getCompletePercentum())+"%");
    }

    public void commandAction(Command c, Displayable d) {
        if(c==complete)
//            controller.handleCommand(Controller.SHOW_LIST_COUNTER, null);
            controller.handleCommand(Controller.DO_UPDATE_COUNTER, counter);
        if(c==count){
            if(counter.getCompleteConut() == 0 || counter.getCompletePercentum() < 100)
            count();
        }
//        if(c==edit)
//            controller.handleCommand(Controller.SHOW_JOB_COUNTER, counter);
//        if(c==remove)
//            controller.handleCommand(Controller.DO_REMOVE_COUNTER, counter);
    }

    protected void paint(Graphics g) {
        y = 0;
       clearBaceground(g);
       drawCounter(g);
    }

    private void clearBaceground(Graphics g){
        int color = g.getColor();
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(color);
    }

    private void drawCountText(Graphics g,int fontSize, String str) {
        y = y + f.getHeight();
        setSize(fontSize);
        g.setFont(f);
        g.drawString(str, x, y, Graphics.HCENTER | Graphics.TOP);
    }

    private void setSize(int size){
            f=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,size);
        }

    private void drawCounter(Graphics g){        
        Font fo = g.getFont();
        drawCountText( g, Font.SIZE_LARGE,counter.getCounterName());
        drawCountText( g, Font.SIZE_LARGE,"需要完成"+String.valueOf(counter.getScheduleCount())+"次");
//        drawCountText( g, Font.SIZE_LARGE,String.valueOf(counter.getScheduleCount()));
        drawCountText( g, Font.SIZE_LARGE,"已经完成");
        drawCountText( g, Font.SIZE_LARGE,String.valueOf(counter.getCompleteConut()));
        drawCountText( g, Font.SIZE_LARGE,"已经完成"+String.valueOf(counter.getCompletePercentum()) + " %");
//        drawCountText( g, Font.SIZE_LARGE,String.valueOf(counter.getCompletePercentum()) + " %");
        g.setFont(fo);
    }

    protected void keyPressed(int keyCode){
        int action = getGameAction(keyCode);
        switch(action){
            case Canvas.FIRE:
                if(counter.getCompleteConut() != 0 && counter.getCompletePercentum() >= 100){break;}
                count();
                break;
        }        
    }

    void count(){
        this.counter.setCompleteConut(counter.getCompleteConut() + 1);
        double i = counter.getCompleteConut();
        double j = counter.getScheduleCount();
        if (j != 0){
            double x1 = i/j;
            int ii =  (int) (x1 * 1000);
            double iii = (double)ii / 10;
            this.counter.setCompletePercentum(iii);
            if(iii == 100.0){
                controller.playCompleteMinuc();
            }
        }
        repaint();
    }
}


